import Vue from 'vue'
import store from '../store'
import VueRouter from 'vue-router'
import { errorAlert, successAlert } from '../utils/alert'


Vue.use(VueRouter)

//验证某个路由
function check(path, next) {
  if (store.state.userInfo.menus_url.find(item => item == path)) {
    next()
  } else {
    errorAlert("您没有相应的权限，请联系管理员")
    next("/index/home")
  }
}

export const routerPath = [
  {
    path: 'menu',
    component: () => import('../pages/menu/menu'),
    meta: {
      title: "菜单管理"
    },
    beforeEnter(to, from, next) {
      check("menu", next)
    }
  },
  {
    path: 'role',
    component: () => import('../pages/role/role'),
    meta: {
      title: "角色管理"
    },
    beforeEnter(to, from, next) {
      check("role", next)
    }
  },
  {
    path: 'manage',
    component: () => import('../pages/manage/manage'),
    meta: {
      title: "管理员管理"
    },
    beforeEnter(to, from, next) {
      check("manage", next)
    }
  },
  {
    path: 'cate',
    component: () => import('../pages/cate/cate'),
    meta: {
      title: "商品分类"
    },
    beforeEnter(to, from, next) {
      check("cate", next)
    }
  },
  {
    path: 'specs',
    component: () => import('../pages/specs/specs'),
    meta: {
      title: "商品规格"
    },
    beforeEnter(to, from, next) {
      check("specs", next)
    }
  },
  {
    path: 'goods',
    component: () => import('../pages/goods/goods'),
    meta: {
      title: "商品管理"
    },
    beforeEnter(to, from, next) {
      check("goods", next)
    }
  },
  {
    path: 'banner',
    component: () => import('../pages/banner/banner'),
    meta: {
      title: "轮播图管理"
    },
    beforeEnter(to, from, next) {
      check("banner", next)
    }
  },
  {
    path: 'seckill',
    component: () => import('../pages/seckill/seckill'),
    meta: {
      title: "秒杀活动"
    },
    beforeEnter(to, from, next) {
      check("seckill", next)
    }
  },
  {
    path: 'member',
    component: () => import('../pages/member/member'),
    meta: {
      title: "会员管理"
    },
    beforeEnter(to, from, next) {
      check("member", next)
    }
  },
]
const routes = [{
  path: "/login",
  component: () => import('../pages/login/login')
},
{
  path: "/index",
  component: () => import('../pages/index/index'),
  children: [{
    path: 'home',
    component: () => import('../pages/home/home')
  },
  ...routerPath,
  {
    path: '',
    redirect: 'home'
  }
  ]
},
{
  path: "*",
  redirect: "/login"
},
]

const router = new VueRouter({
  routes
})
//登录拦截
router.beforeEach((to, from, next) => {
  if (to.path == "/login") {
    next()
    return;
  }
  if (store.state.userInfo.token) {
    next()
    return
  }
  next("/login")
})
export default router